package com.bianmaba.spring.security.basic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@ConfigurationProperties(prefix = "security")
@ConditionalOnProperty(prefix = "security.basic", name = "enabled", matchIfMissing = true)
@ConditionalOnMissingBean(BasicSecurityProperties.class)
public class BasicSecurityProperties {
    private static final Logger LOG = LoggerFactory.getLogger(BasicSecurityProperties.class);
    private String[] DEFAULT_IGNORING = new String[]{"/css/**", "/js/**", "/images/**", "/webjars/**", "/**/favicon.ico", "/**/*.css", "/**/*.js", "/**/*.jpg", "/**/*.png", "/**/*.gif", "/**/*.bmp", "/**/*.icon", "/**/*.ico", "/**/*.swf"};
    private List<String> permited = new ArrayList<String>(0);
    private String loginPage = null;
    private String loginSuccessUrl = null;
    private String logoutSuccessUrl = null;
    private boolean anyRequestAuthenticated = false;
    private boolean ajax = false;
    private WebSecurityBasic basic = new WebSecurityBasic();
    @Autowired
    private SecurityProperties securityProperties;

    public List<String> getPermited() {
        return permited;
    }

    public void setPermited(List<String> permited) {
        this.permited = permited;
    }

    public List<String> getIgnored() {
        List<String> defaults = Arrays.asList(DEFAULT_IGNORING);
        securityProperties.getIgnored().removeAll(defaults);
        securityProperties.getIgnored().addAll(defaults);
        return securityProperties.getIgnored();
    }

    public WebSecurityBasic getBasic() {
        return basic;
    }

    public void setBasic(WebSecurityBasic basic) {
        this.basic = basic;
    }

    public String getLoginPage() {
        return loginPage;
    }

    public void setLoginPage(String loginPage) {
        this.loginPage = loginPage;
    }

    public String getLoginSuccessUrl() {
        return loginSuccessUrl;
    }

    public void setLoginSuccessUrl(String loginSuccessUrl) {
        this.loginSuccessUrl = loginSuccessUrl;
    }

    public String getLogoutSuccessUrl() {
        return logoutSuccessUrl;
    }

    public void setLogoutSuccessUrl(String logoutSuccessUrl) {
        this.logoutSuccessUrl = logoutSuccessUrl;
    }

    public boolean isAjax() {
        return ajax;
    }

    public void setAjax(boolean ajax) {
        this.ajax = ajax;
    }

    public boolean isRequireSsl() {
        return securityProperties.isRequireSsl();
    }

    public boolean isEnableCsrf() {
        return securityProperties.isEnableCsrf();
    }

    public SecurityProperties.Headers getHeaders() {
        return securityProperties.getHeaders();
    }

    public boolean isAnyRequestAuthenticated() {
        return anyRequestAuthenticated;
    }

    public void setAnyRequestAuthenticated(boolean anyRequestAuthenticated) {
        this.anyRequestAuthenticated = anyRequestAuthenticated;
    }

    public static class WebSecurityBasic extends SecurityProperties.Basic {
        private List<Map<String, String>> users = new ArrayList<Map<String, String>>(0);

        private List<Map<String, String>> resources = new ArrayList<Map<String, String>>(0);

        public List<Map<String, String>> getUsers() {
            return users;
        }

        public void setUsers(List<Map<String, String>> users) {
            this.users = users;
        }

        public List<Map<String, String>> getResources() {
            return resources;
        }

        public void setResources(List<Map<String, String>> resources) {
            this.resources = resources;
        }
    }
}
